package com.three.iot.jdbc.dao;

import com.three.iot.commons.enums.DictTypeEnum;
import com.three.iot.jdbc.model.SysDictInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Date:2017/11/5 0005 15:52
 * @Author lu.dong
 * @Description：
 **/
@Component
public class DictUtils {
    private static final Logger logger = LoggerFactory.getLogger(DictUtils.class);
    @Resource
    private DictDao dictDao;

    private static Map<String, List<SysDictInfo>> dictTypeMap = new HashMap<String, List<SysDictInfo>>();

    private static Map<String, SysDictInfo> dictCodeMap = new HashMap<String, SysDictInfo>();

    private static DictDao staticDictDao;
    //是否缓存
    @Value("${three.dict.enableCache:N}")
    private  String enableCache;

    private  static String enableDictCache="N";
    @PostConstruct
    //@Scheduled(cron = "0 0 * * * ?")
    public void loadDict() {
        enableDictCache=enableCache;
        logger.info("==================数据字典初始化Begin=enableDictCache:{}",enableDictCache);
        dictTypeMap.clear();
        dictCodeMap.clear();

        if("Y".equals(enableCache)){
            List<SysDictInfo> sysDictList = dictDao.findAll();
            putDictTypeMap(sysDictList);
            putdictCodeMap(sysDictList);
        }
        staticDictDao = dictDao;
        logger.info("==================数据字典初始化End=========================");
    }

    /**
     * 按照type,code存储
     *
     * @param sysDictList
     */
    private void putdictCodeMap(List<SysDictInfo> sysDictList) {
        for (SysDictInfo sysDict : sysDictList) {
            String key = sysDict.getPdictNo() + "-" + sysDict.getDictNo();
            dictCodeMap.put(key, sysDict);
        }
    }

    /**
     * 按照类型存储
     *
     * @param sysDictList
     */
    private void putDictTypeMap(List<SysDictInfo> sysDictList) {
        for (SysDictInfo sysDict : sysDictList) {
            if (!dictTypeMap.containsKey(sysDict.getPdictNo())) {
                List dictLists = new ArrayList();
                dictLists.add(sysDict);
                dictTypeMap.put(sysDict.getPdictNo(), dictLists);
            } else {
                List oldDictLists = dictTypeMap.get(sysDict.getPdictNo());
                oldDictLists.add(sysDict);
                dictTypeMap.put(sysDict.getPdictNo(), oldDictLists);
            }
        }
    }

    public static List<SysDictInfo> getValueByType(String dictType) {
        if("Y".equals(enableDictCache)) {
            List<SysDictInfo> SysDictInfos = dictTypeMap.get(dictType);
            if (SysDictInfos == null || SysDictInfos.size() == 0) {
                SysDictInfos = staticDictDao.findByPDict(dictType);
                dictTypeMap.put(dictType, SysDictInfos);
            }
            return SysDictInfos;
        }
       return staticDictDao.findByPDict(dictType);
    }

    public static SysDictInfo getValueByUniq(String dictType, String dictCode) {
        String key = dictType + "-" + dictCode;
        if("Y".equals(enableDictCache)) {
            SysDictInfo SysDictInfo = dictCodeMap.get(key);
            if (SysDictInfo == null) {
                SysDictInfo = staticDictDao.findByUniq(dictType, dictCode);
                dictCodeMap.put(key, SysDictInfo);
            }
            return SysDictInfo;
        }
        return staticDictDao.findByUniq(dictType, dictCode);
    }

    public static String getDictValueByUniq(String dictType, String dictCode) {
        SysDictInfo sysDict = getValueByUniq(dictType, dictCode);
        if (sysDict == null) return "";
        return sysDict.getDictVal();
    }

    public static String getDictValueByUniq(DictTypeEnum dictTypeEnum, String dictCode) {
        return getDictValueByUniq(dictTypeEnum.getCode(),dictCode);
    }

    public static String getNameByUniq(String dictType, String dictCode) {
        SysDictInfo sysDict = getValueByUniq(dictType, dictCode);
        if (sysDict == null) return "";
        return sysDict.getDictName();
    }

    public static String getNameByUniq(DictTypeEnum dictTypeEnum, String dictCode) {
        return getNameByUniq(dictTypeEnum.getCode(),dictCode);
    }

}
